import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  ParseIntPipe,
  HttpStatus,
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import {
  ApiBody,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { ChangePasswordDto } from './dto/change-password.dto';
// import { RequirePermission } from '../permission/decorators/public.decorator';

@ApiTags('用户管理')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}
  // 通过 @ApiOperation 描述接口的信息，@ApiResponse 描述返回值信息，@ApiQuery 描述 query 参数信息。
  @ApiOperation({ summary: '创建用户', description: '创建用户接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: 'ccc 成功',
    type: CreateUserDto,
  })
  @ApiBody({
    type: CreateUserDto,
  })
  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  // @RequirePermission('查询用户权限')
  @ApiOperation({ summary: '查询用户列表' })
  @ApiQuery({
    name: 'page',
    type: Number,
    description: '当前页码',
    required: false,
    example: 1,
  })
  @ApiQuery({
    name: 'pageSize',
    type: Number,
    description: '分页大小',
    required: true,
    example: 10,
  })
  @Get()
  findAll(
    @Query('page', new ParseIntPipe()) page = 1,
    @Query('pageSize', new ParseIntPipe()) pageSize = 10,
  ) {
    return this.userService.findAll({ page, pageSize });
  }

  @ApiOperation({ summary: '查询用户列表' })
  @ApiQuery({
    name: 'page',
    type: Number,
    description: '当前页码',
    required: false,
    example: 1,
  })
  @ApiQuery({
    name: 'pageSize',
    type: Number,
    description: '分页大小',
    required: true,
    example: 10,
  })
  @ApiQuery({
    name: 'username',
    type: String,
    description: '用户名',
    required: false,
    example: 'admin',
  })
  @ApiQuery({
    name: 'nickname',
    type: String,
    description: '昵称',
    required: false,
    example: '',
  })
  @ApiQuery({
    name: 'phone',
    type: String,
    description: '用户手机',
    required: false,
    example: '1008611',
  })
  @ApiQuery({
    name: 'gender',
    type: Number,
    description: '性别[1:男,2:女,-1:未知]',
    required: false,
    example: -1,
  })
  @Get('page')
  pageSearch(
    @Query('page', new ParseIntPipe()) page = 1,
    @Query('pageSize', new ParseIntPipe()) pageSize = 10,
    @Query('username') username?: string,
    @Query('nickname') nickname?: string,
    @Query('phone') phone?: string,
    @Query('gender') gender?:string,
  ) {
    
    return this.userService.pageSearch({
      page,
      pageSize,
      username,
      nickname,
      phone,
      gender: gender,
    });
  }

  @ApiOperation({ summary: '查询用户详情' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '查询用户详情成功',
    type: String,
  })
  @ApiResponse({
    status: HttpStatus.UNAUTHORIZED,
    description: 'id不合法',
  })
  @ApiParam({
    name: 'id',
    description: '用户ID',
    required: true,
    example: {},
  })
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.userService.findOne(id);
  }

  @ApiOperation({ summary: '更新用户信息' })
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(id, updateUserDto);
  }

  @ApiOperation({ summary: '删除用户' })
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(id);
  }

  @ApiOperation({ summary: '强制修改用户密码' })
  @Patch('modifyPwd/:id')
  modifyPassword(@Param('id') id: string, @Body() dto: ChangePasswordDto) {
    return this.userService.modifyPassword(id, dto);
  }

  @ApiOperation({ summary: '用户修改自己的密码' })
  @Patch('updPwd/:id')
  updateUserPwd(@Param('id') id: string, @Body() dto: ChangePasswordDto) {
    return this.userService.updateUserPwd(id, dto);
  }
}
